1368D - AND OR and square sum - CodeForces Solution


bitmasks greedy math *1700

Please click on ads to support us..

C++ Code:

//#include"pch.h"
#include<bits/stdc++.h>
using namespace std;
#define Mbappe {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
#define ll long long
#define vc vector
#define str string
#define e "\n"
#define all(v) (v).begin() , (v).end() 
#define File freopen("problem name", "r", stdin);
#define pi 3.14159265359
#define MEM(x , y) memset(x , y , sizeof(x))
const int mod = 1e9 + 7;
const int MOD = 32768;
const int oo = 0x3f3f3f3f;
bool ok = true, go = true;
int dx[8] = { 0, 0, 1, -1, -1, -1, 1, 1 };
int dy[8] = { -1, 1, 0, 0, -1, 1, -1, 1 };
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
//---------------------------- [ We need a trick ] -------------------------</end>
/*
const int N = 100;
vc<vc<int>>adj(N);// about graph repressentation
vc<vc<int>>ccs;// collect components
vc<int> instack(N);// for chieck this node exist in ancestor or not
vc<int>DFS_num(N);// numbring the nodes with DFS number
vc<int>lwlink(N);// numbring the nodes with low link number
vc<int>any_comp(N);// the node exist in any component
stack<int>stk;// for get all nodes in one component
int ini = 1;// inatail value of both DFS_num and lowlink number
void Uncle_Tarjan_Ccs(int node) {
	DFS_num[node] = ini; lwlink[node] = ini; instack[node] = 1; stk.push(node); ini++;// intailize
	for (auto u : adj[node]) {
		if (!DFS_num[u]) {// not visted
			Uncle_Tarjan_Ccs(u);
			lwlink[node] = min(lwlink[node], lwlink[u]);
		}
		else if (instack[u]) {// visted
			lwlink[node] = min(lwlink[node], lwlink[u]);
		}
	}
	// this node finished all his childs
	if (lwlink[node] == DFS_num[node]) {
		int x = -1;
		ccs.push_back(vc<int>());
		while (x!= node)
		{
			x = stk.top(); stk.pop(); instack[x] = 0;
			ccs.back().push_back(x);
			any_comp[x] = ccs.size();
		}
	}
}

const int N = 2e5+5;
vc<vc<int>>adj(N);
vc<int>DFS_num(N), lw(N);
vc<pair<int, int>>bridges;
int ini = 1;
void Uncle_Tarjan_Bridge(int node , int par) {
	DFS_num[node] = ini; lw[node] = ini; ini++;
	for (auto u : adj[node]) {
		if (!DFS_num[u]) {
			Uncle_Tarjan_Bridge(u, node);
			lw[node] = min(lw[node], lw[u]);
		}
		else if (u != par) {
			lw[node] = min(lw[node], lw[u]);
		}
	}
	if (DFS_num[node] == lw[node] && par != -1 ) {
		//cout << "exist bridge here from " << par << " to " << node << e;
		bridges.push_back({ par , node });
	}
}
*/
int main() {
	Mbappe;
	int n; cin >> n;
	ll ans = 0;
	map<int, int>mp;
	for (int a = 0; a < n; a++) {
		int x; cin >> x;
		for (int z = 0; z <= 20; z++) {
			if (x & (1 << z)) {
				int num = (1 << z);
				mp[num]++;
			}
		}
	}
	ok = true;
	while (ok)
	{
		ok = false;
		ll cur = 0;
		for (auto u : mp) {
			if (mp[u.first]) {
				cur += u.first;
				mp[u.first]--;
				ok = true;
			}
		}
		ans += (cur * cur);
	}
	cout << ans;
}


Comments

Submit
0 Comments
More Questions

1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets
1691B - Shoe Shuffling
1706A - Another String Minimization Problem
1695B - Circle Game
1702B - Polycarp Writes a String from Memory
1701A - Grass Field
489C - Given Length and Sum of Digits
886B - Vlad and Cafes
915A - Garden
356A - Knight Tournament
1330A - Dreamoon and Ranking Collection
1692B - All Distinct
1156C - Match Points
1675A - Food for Animals
1328C - Ternary XOR
1689A - Lex String